package cn.wecloud.im.multiplayervideodemo.live

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Rect
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import cn.wecloud.im.multiplayervideodemo.R
import cn.wecloud.im.multiplayervideodemo.utils.DisplayUtils
import cn.wecloud.im.multiplayervideodemo.utils.GlideApp
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import io.livekit.android.VideoMember
import jp.wasabeef.glide.transformations.BlurTransformation
import kotlinx.android.synthetic.main.item_fold_photo.view.*
import kotlinx.android.synthetic.main.view_comming_call_out.view.*
import kotlin.math.min

/**
 * 去电页面
 */
class CommingCallOutView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : FrameLayout(context, attrs, defStyleAttr) {

    private var mOnActionListener: OnActionListener? = null
    private val adapter = RecyViewAdapter()
    private var users = mutableListOf<String>()

    init {
        LayoutInflater.from(context).inflate(R.layout.view_comming_call_out, this)
        tvVideoVoice.isSelected = false
        tvVideoVoice?.setOnClickListener { v ->
            //静音
            v.isSelected = !v.isSelected
            mOnActionListener?.onVoiceToggleClick(v)
        }
        tvVideoSpeaker.isSelected = true
        tvVideoSpeaker?.setOnClickListener { v ->
            //扬声器
            v.isSelected = !v.isSelected
            mOnActionListener?.onSpeakerToggleClick(v)
        }
        tvCallVideoHangUp?.setOnClickListener { v ->
            //拒接
            mOnActionListener?.onRejectClick(v)

        }
        tvCallVideoCamera.isSelected = true
        tvCallVideoCamera?.setOnClickListener { v ->
            //摄像头
            v.isSelected = !v.isSelected
            mOnActionListener?.onCameraToggleClick(v)
        }
        ivCallVideoMin?.setOnClickListener { v ->
            //最小化
            mOnActionListener?.onScreenMinClick(v)
        }

        ivCallAvatar.adapter = adapter
        ivCallAvatar.addItemDecoration(object : RecyclerView.ItemDecoration() {
            override fun getItemOffsets(
                outRect: Rect,
                view: View,
                parent: RecyclerView,
                state: RecyclerView.State
            ) {
                super.getItemOffsets(outRect, view, parent, state)
                val position = parent.getChildAdapterPosition(view)
                if (position < min(users.size, maxLength) - 1) {
                    outRect.left = DisplayUtils.dp2px(context, -27f)
                }
            }
        })
        val lm = ivCallAvatar.layoutManager
        if (lm is LinearLayoutManager) {
            lm.reverseLayout = true
            lm.stackFromEnd = true
        }

    }

    fun setOnActionListener(onActionListener: OnActionListener) {
        mOnActionListener = onActionListener
    }

    @SuppressLint("NotifyDataSetChanged")
    fun setInviteeUsersInfo(inviteeUsers: List<VideoMember>?) {
        users.clear()
        val invitee = inviteeUsers?.mapNotNull { it.clientId }
        if (invitee == null || invitee.isEmpty()) {
            return
        }
        users.addAll(invitee)
        adapter.notifyDataSetChanged()
        GlideApp.with(context).load(invitee[0]).centerCrop()
            .transform(CenterCrop(), BlurTransformation(25, 3))
            .into(ivCallBgAvatar)
    }

    interface OnActionListener {
        fun onRejectClick(v: View?)
        fun onCameraToggleClick(v: View?)
        fun onVoiceToggleClick(v: View?)
        fun onSpeakerToggleClick(v: View?)
        fun onScreenMinClick(v: View?)
    }

    val maxLength = 6

    inner class RecyViewAdapter : RecyclerView.Adapter<RecyViewAdapter.ViewHolder>() {


        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
            return ViewHolder(
                LayoutInflater.from(context).inflate(R.layout.item_fold_photo, parent, false)
            )
        }

        override fun onBindViewHolder(holder: ViewHolder, position: Int) {
            if (position == 0 && users.size >= maxLength) {
                GlideApp.with(context).load(R.mipmap.icon_photo_end)
                    .circleCrop()
                    .placeholder(R.mipmap.ic_avatar_1)
                    .into(holder.itemView.item)
            } else {
                GlideApp.with(context).load("")
                    .circleCrop()
                    .placeholder(R.mipmap.ic_avatar_1)
                    .into(holder.itemView.item)
            }
        }

        override fun getItemCount(): Int {
            return min(users.size, maxLength)
        }

        inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)
    }
}